package com.ruoyi.psi.inventory.check.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.inventory.check.domain.InventoryCheck;
import com.ruoyi.psi.inventory.check.domain.InventoryCheckList;
import com.ruoyi.psi.inventory.check.mapper.InventoryCheckMapper;
import com.ruoyi.psi.inventory.check.utils.CheckUtils;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存盘点
 * 业务层执行
 */
@Service
public class InventoryCheckServiceImpl implements IInventoryCheckService {
    @Autowired
    private InventoryCheckMapper inventoryCheckMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;


    /**
     * 仓库盘点查询
     *
     * @param inventoryCheck
     * @return
     */
    @Override
    public List<InventoryCheck> selectInventoryCheckList(InventoryCheck inventoryCheck) {
        List<InventoryCheck> inventoryChecks = inventoryCheckMapper.selectInventoryCheckList(inventoryCheck);
        for (int i = 0; i < inventoryChecks.size(); i++) {
            if (StringUtils.isNotNull(inventoryCheckMapper.selectInventoryCheckListListById(inventoryChecks.get(i).getInventoryCheckId()))) {
                //存在，则存出仓库盘点对应产品信息
                inventoryChecks.get(i).setInventoryCheckLists(inventoryCheckMapper.selectInventoryCheckListListById(inventoryChecks.get(i).getInventoryCheckId()));
            }
        }
        return inventoryChecks;
    }

    /**
     * 按编号查询
     *
     * @param inventoryCheckId 仓库盘点编号
     * @return
     */
    @Override
    public InventoryCheck selectInventoryCheckById(String inventoryCheckId) {
        return inventoryCheckMapper.selectInventoryCheckById(inventoryCheckId);
    }

    /**
     * 仓库盘点新增
     *
     * @param inventoryCheck
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInventoryCheck(InventoryCheck inventoryCheck) throws Exception {
        String inventoryCheckId = idUtil.createInventoryCheckId(); //自动生成编号
        if (StringUtils.isNotNull(inventoryCheckMapper.selectInventoryCheckById(inventoryCheckId))) {
            //存在
            return 0;
        }
        //不存在
        inventoryCheck.setInventoryCheckId(inventoryCheckId); //主表添加编号
        inventoryCheckMapper.insertInventoryCheck(inventoryCheck);//仓库盘点据
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryCheck.getWarehouseId()); // 查询该仓库的产品列表
        List<InventoryCheckList> inventoryCheckLists = CheckUtils.removeByCheckAfterNumber(inventoryCheck.getInventoryCheckLists());//仓库盘点产品信息
        for (int i = 0; i < inventoryCheckLists.size(); i++) {
            inventoryCheckLists.get(i).setInventoryCheckId(inventoryCheckId); //子表添加编号
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
            warehouseListDomain.setWarehouseId(inventoryCheck.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryCheckLists.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryCheckLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 产品盘点前数量 + 产品盘点后数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryCheckLists.get(i).getCheckBeforeNumber() + inventoryCheckLists.get(i).getCheckAfterNumber());
                    break; //终止循环
                }
            }
            //更新库存
            warehouseMapper.updateWarehouseList(warehouseListDomain);
            inventoryCheckMapper.insertInventoryCheckList(inventoryCheckLists.get(i));//仓库盘点产品信息
        }
        return 1;
    }

    /**
     * 仓库盘点更新
     *
     * @param inventoryCheck
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInventoryCheck(InventoryCheck inventoryCheck) throws Exception {
        if (!StringUtils.isNotNull(inventoryCheckMapper.selectInventoryCheckById(inventoryCheck.getInventoryCheckId()))) {
            //不存在
            return 0;
        }
        //存在仓库盘点单据
        inventoryCheckMapper.updateInventoryCheck(inventoryCheck);//仓库盘点单据
        List<InventoryCheckList> inventoryCheckListOld = inventoryCheckMapper.selectInventoryCheckListListById(inventoryCheck.getInventoryCheckId());
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryCheck.getWarehouseId()); // 查询该仓库的产品列表
        List<InventoryCheckList> inventoryCheckListNew = CheckUtils.removeByCheckAfterNumber(inventoryCheck.getInventoryCheckLists());//仓库盘点产品信息
        for (int i = 0; i < inventoryCheckListNew.size(); i++) {
            InventoryCheckList inventoryCheckList = inventoryCheckMapper.selectInventoryCheckListById(inventoryCheck.getInventoryCheckId(), inventoryCheckListNew.get(i).getProduceId());
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryCheck.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryCheckListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(inventoryCheckList)) {
                for (int j = 0; j < inventoryCheckListOld.size(); j++) {
                    if (inventoryCheckListOld.get(j).getProduceId().equals(inventoryCheckList.getProduceId())) {
                        inventoryCheckListOld.remove(j);
                        break;
                    }
                }
                //存在仓库盘点详细
                //更新
                for (WarehouseListDto s : warehouseListDtos) {
                    // 遍历仓库
                    if (inventoryCheckListNew.get(i).getProduceId().equals(s.getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 原盘点之前的数量 - 原盘点之后的数量 - 修改之后的盘点前数量 + 修改之后的盘点后数量
                        warehouseListDomain.setProductNumber(s.getProductNumber() + inventoryCheckList.getCheckBeforeNumber() - inventoryCheckList.getCheckAfterNumber() - inventoryCheckListNew.get(i).getCheckBeforeNumber() + inventoryCheckListNew.get(i).getCheckAfterNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
                inventoryCheckMapper.updateInventoryCheckList(inventoryCheckListNew.get(i));
            } else {//新增
                // 子表中不存在该产品，更新库存数，库存数 = 该产品新增时的入库数量
                for (WarehouseListDto s : warehouseListDtos) {
                    // 遍历仓库
                    if (inventoryCheckListNew.get(i).getProduceId().equals(s.getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 新修改的入库数量
                        warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryCheckListNew.get(i).getCheckBeforeNumber() + inventoryCheckListNew.get(i).getCheckAfterNumber());
                        break; //终止循环
                    }
                }
                // 该更新库存数量
                warehouseMapper.updateWarehouseList(warehouseListDomain);
                inventoryCheckMapper.insertInventoryCheckList(inventoryCheckListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < inventoryCheckListOld.size(); i++) {
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryCheck.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryCheckListOld.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryCheckListOld.get(i).getProduceId().equals(s.getProduceId())) {
                    // 删除时，产品库存数 = 原有库存 - 盘点后的数量 + 盘点前的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryCheckListOld.get(i).getCheckAfterNumber() + inventoryCheckListOld.get(i).getCheckBeforeNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
            //最后将要删除的子标记录删除
            inventoryCheckMapper.deleteInventoryCheckListById(inventoryCheckListOld.get(i).getInventoryCheckId(), inventoryCheckListOld.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新仓库盘点删除标志位
     *
     * @param inventoryCheck
     * @return
     */
    @Override
    public int updateInventoryCheckDeleteFlag(InventoryCheck inventoryCheck) {
        List<InventoryCheckList> inventoryCheckLists = inventoryCheck.getInventoryCheckLists();//被删除记录的所有子表记录
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryCheck.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < inventoryCheckLists.size(); i++) {
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryCheck.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryCheckLists.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryCheckLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 删除时，删除时，产品库存数 = 原有库存 - 盘点后的数量 + 盘点前的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryCheckLists.get(i).getCheckAfterNumber() + inventoryCheckLists.get(i).getCheckBeforeNumber());
                    break;
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
        }
        return inventoryCheckMapper.updateInventoryCheckDeleteFlag(inventoryCheck);
    }

    /**
     * 仓库盘点详细查询
     *
     * @param inventoryCheckList
     * @return
     */
    @Override
    public List<InventoryCheckList> selectInventoryCheckListList(InventoryCheckList inventoryCheckList) {
        return inventoryCheckMapper.selectInventoryCheckListList(inventoryCheckList);
    }

    /**
     * 查询仓库盘点对应的所有产品详细
     *
     * @param inventoryCheckId 仓库盘点编号
     * @return
     */
    @Override
    public List<InventoryCheckList> selectInventoryCheckListListById(String inventoryCheckId) {
        return inventoryCheckMapper.selectInventoryCheckListListById(inventoryCheckId);
    }

    /**
     * 查询仓库盘点对应的指定产品详细
     *
     * @param inventoryCheckId
     * @param produceId
     * @return
     */
    @Override
    public InventoryCheckList selectInventoryCheckListById(String inventoryCheckId, String produceId) {
        return inventoryCheckMapper.selectInventoryCheckListById(inventoryCheckId, produceId);
    }
}
